local lihun = fk.CreateSkill {

  name = "joy__lihun",

  tags = {  },

}



lihun:addEffect("active", {
  name = "joy__lihun",
  mute = true,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(lihun.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 
  end,
  target_filter = function(self, player, to_select, selected)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and
    not target:isKongcheng() and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    player:broadcastSkillInvoke(lihun.name, 1)
    room:notifySkillInvoked(player, lihun.name, "control")
    local mark = player:getMark("joy__lihun-phase")
    if mark == 0 then mark = {} end
    table.insertIfNeed(mark, target.id)
    room:setPlayerMark(player, "joy__lihun-phase", mark)
    player:turnOver()
    if player.dead or target.dead or target:isKongcheng() then return end
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(target:getCardIds("h"))
    room:moveCardTo(dummy,Card.PlayerHand,player,fk.ReasonPrey,lihun.name,nil,false,player)
  end,
})

lihun:addEffect(fk.EventPhaseEnd, {
  name = "#joy__lihun_record",
  mute = true,
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Play and player:getMark("joy__lihun-phase") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("joy__lihun", 2)
    room:notifySkillInvoked(player, "joy__lihun", "control")
    local mark = player:getMark("joy__lihun-phase")
    for _, id in ipairs(mark) do
      if player.dead or player:isNude() then return end
      local to = room:getPlayerById(id)
      if not to.dead then
        local cards = player:getCardIds("he")
        local n = to.hp
        if n < #cards then
          cards = room:askForCard(player, n, n, true, "joy__lihun", false, ".", "#lihun-give::"..to.id..":"..n)
        end
        room:moveCardTo(cards, Card.PlayerHand, to, fk.ReasonGive, "joy__lihun", nil, false, player.id)
      end
    end
  end,
})

return lihun